package com.atguigu.gmall.list.listener;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.list.service.ListService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 李旭
 * @date 2021/12/3 14:41
 * @Description:
 *
 *   搜索微服务的监听器
 */
@Component
public class ListListener {


    @Autowired
    private ListService listService;

    //监听消息
    //上架
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_UPPER}
    )})
    public void onSale(Message message, Channel channel,Long skuId){
        try {
            System.out.println("接收到的消息是：" + skuId);
            //上架 添加索引
            listService.onSale(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //e.printStackTrace();
            //是否第一次投递  如果是再投递  如果不是就拒绝投递
            //....................service-mq微服务已经写过了
        }
    }

    /**
     * 下架
     * @param message
     * @param channel
     * @param skuId
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_LOWER}
    )})
    public void cancelSale(Message message, Channel channel,Long skuId){
        try {
            System.out.println("接收到的消息是：" + skuId);
            //下架 删除索引
            listService.cancelSale(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //e.printStackTrace();
            //是否第一次投递  如果是再投递  如果不是就拒绝投递
            //....................service-mq微服务已经写过了
        }
    }
}
